import { useDictionaryStore } from '@/store/modules/dictionary'
import { computed, ComputedRef } from 'vue'

const dictionaryStore = useDictionaryStore()

interface SelectorConfig {
	key: string
	type: string | null
	field: string
	label: string
}

export interface Label {
	type: string
	typeName: string
	options: Option[]
}

export interface Option {
	id: string
	name: string
	remark: string
	type: number
	weight: number
}

export interface Category {
	id: string
	standardName: string
	standardType: string
	standardTypeValue: string
	standardLevel: string
	standardLevelValue: string
	pid: string
	weight: number
	remark: string | null
	hasChosen: boolean
	children: Category[]
}

interface Selectors {
	mandatoryCategory: ComputedRef<Label>
	standardCategory: ComputedRef<Label>
	standardStatus: ComputedRef<Label>
	compileStatus: ComputedRef<Label>
	engineeringMajor: ComputedRef<Label>
	lifecycleCategory: ComputedRef<Label>
	managementLine: ComputedRef<Label>
	buildingType: ComputedRef<Label>
	yangtzeRiverTag: ComputedRef<Label>
	internationalField: ComputedRef<Label>
	hotField: ComputedRef<Label>
	basicCategories: ComputedRef<Category[]>
	specialCategories: ComputedRef<Category[]>
}

/**
 * 创建选择器
 * @param type 类型标识符
 * @param field 字段名
 * @returns 计算属性
 */
const createSelector = <T extends Label | Category[]>(type: string | null, field: string): ComputedRef<T> => {
	return computed(() => {
		const items = dictionaryStore.dictionaryCategory[field as keyof typeof dictionaryStore.dictionaryCategory]
		if (!items) {
			console.error(`Field ${field} not found in dictionaryCategory`)
			return null as T
		}
		if (type) {
			if (!Array.isArray(items)) {
				console.error(`Expected array for field ${field}`)
				return null as T
			}
			const result = items.find(item => 'type' in item && item.type === type)
			if (!result) {
				console.error(`Type ${type} not found in field ${field}`)
				return null as T
			}
			return result as T
		}
		return items as T
	})
}

const selectorsConfig: SelectorConfig[] = [
	{ key: 'mandatoryCategory', type: '4', field: 'labels', label: '强制性分类' },
	{ key: 'standardCategory', type: '1', field: 'labels', label: '标准类别' },
	{ key: 'standardStatus', type: '2', field: 'labels', label: '标准状态' },
	{ key: 'compileStatus', type: '3', field: 'labels', label: '编制状态' },
	{ key: 'basicCategories', type: null, field: 'basicCategories', label: '基础分类' },
	{ key: 'specialCategories', type: null, field: 'specialCategories', label: '专题分类' },
	{ key: 'engineeringMajor', type: '7', field: 'labels', label: '工程专业分类' },
	{ key: 'lifecycleCategory', type: '5', field: 'labels', label: '工程全生命周期分类' },
	{ key: 'managementLine', type: '8', field: 'labels', label: '管理条线分类' },
	{ key: 'buildingType', type: '6', field: 'labels', label: '建筑类型分类' },
	{ key: 'yangtzeRiverTag', type: '14', field: 'labels', label: '长三角区域表标签' },
	{ key: 'internationalField', type: '12', field: 'labels', label: '标准国际化领域标签' },
	{ key: 'hotField', type: '13', field: 'labels', label: '热门领域标签' }
]

const selectors = selectorsConfig.reduce((acc, { key, type, field }) => {
	const selector =
		key === 'basicCategories' || key === 'specialCategories' ? createSelector<Category[]>(type, field) : createSelector<Label>(type, field)
	return { ...acc, [key]: selector }
}, {} as Selectors)

export const {
	mandatoryCategory,
	standardCategory,
	standardStatus,
	compileStatus,
	basicCategories,
	specialCategories,
	engineeringMajor,
	lifecycleCategory,
	managementLine,
	buildingType,
	yangtzeRiverTag,
	internationalField,
	hotField
} = selectors
